class Solution {
public:
    string reverseWords(string s) {
        int left = 0;
        int right = 0;
        int n = s.size();
        int i = 0;
        while (i < n)
        {
            int state = i;
            while (i < n && s[i] != ' ')
                i++;
            left = state;
            right = i - 1;
            while (left < right)
            {
                swap(s[left], s[right]);
                left++;
                right--;
            }
            while (i < n && s[i] == ' ')
                i++;
        }
        return s;
    }
};